package com.atguigu.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {
    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        this.baseMapper.insert(skuInfoEntity);
    }

    @Override
    public PageUtils queryPageByCondtion(Map<String, Object> params) {
//        sidx: sidx: 'id',//排序字段
//order: 'asc/desc',//排序方式
//key: '华为',//检索关键字
//catelogId: 0,
//brandId: 0,
//min: 0,
//max: 0
        LambdaQueryWrapper<SkuInfoEntity> queryWrapper = new LambdaQueryWrapper<>();
       String key  = (String) params.get("key");
       if(!StringUtils.isEmpty(key)){
           queryWrapper.and(wrapper->wrapper.eq(SkuInfoEntity::getSkuName,key).or().eq(SkuInfoEntity::getSkuId,key));
       }
       String catelogId  = (String) params.get("catelogId");
       if(!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)){
           queryWrapper.eq(SkuInfoEntity::getCatalogId,catelogId);
       }
       String brandId  = (String) params.get("brandId");
       if(!StringUtils.isEmpty(brandId)  && !"0".equalsIgnoreCase(brandId)){
           queryWrapper.eq(SkuInfoEntity::getBrandId,brandId);
       }
       String min  = (String) params.get("min");
       if(!StringUtils.isEmpty(min) && min!=null ){
           queryWrapper.ge(SkuInfoEntity::getPrice,min);
       }
       String max  = (String) params.get("max");
       if(max!=null && !"0".equalsIgnoreCase(max)){
           queryWrapper.le(SkuInfoEntity::getPrice,max);
       }
       String order  = (String) params.get("order");
       if(order!=null ){
           queryWrapper.orderBy(order.equals("asc"),true,SkuInfoEntity::getSkuId);
       }

        return new PageUtils(this.page(new Query<SkuInfoEntity>().getPage(params),queryWrapper)) ;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

}